package com.example.order.activity;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.order.R;
import com.example.order.pojo.Desk;
import com.example.order.pojo.Order;
import com.example.order.utils.Utils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Main5Activity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);
        //查看历史订单
        final ListView listView=findViewById(R.id.list_history_order);
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.support_simple_spinner_dropdown_item,
                new String[]{"没有历史订单"});
        listView.setAdapter(adapter);
        //获取电话号码
        //String phone = getIntent().getStringExtra("phone");
        //获取用户id
        int id = getIntent().getIntExtra("id", 1);
        HashMap<String,String> map=new HashMap<>();
        map.put("id",String.valueOf(id));
        Utils.sendPostHttpRequest("order/findAll", map,
                new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String strings = response.body().string();
                        Log.i("Main5Activity", strings);
                        Gson gson=new Gson();
                        List<Order> orders=gson.fromJson(strings,new TypeToken<List<Order>>(){}.getType());
                        Log.i("Main5Activity", orders.toString());
                       // final List<String> orders2=new ArrayList<>();
                        final List<HashMap<String,String>> data=new ArrayList<>();
                        for (int i = 0; i < orders.size(); i++) {
                           // orders2.add(orders.get(i).toString());
                            HashMap<String,String> hashMap=new HashMap<>();
                            hashMap.put("text1",orders.get(i).toString());
                            hashMap.put("text2","订单时间:"+orders.get(i).getTime().toString());
                            data.add(hashMap);
                        }

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                /*listView.setAdapter( new ArrayAdapter<String>(Main5Activity.this,
                                        R.layout.support_simple_spinner_dropdown_item,
                                       orders2));*/
                                listView.setAdapter(new SimpleAdapter(Main5Activity.this,
                                        data,
                                        android.R.layout.simple_list_item_2,
                                        new String[]{"text1","text2"},
                                        new int[]{android.R.id.text1,android.R.id.text2}));
                            }
                        });

                    }
                });



    }
}
